package com.itlong.cloud.sdk.service.impl;

import com.itlong.cloud.POJO.DTO.sdk.CloudElevatorSDKGetByDeviceUniqueDTO;
import com.itlong.cloud.POJO.DTO.sdk.CloudElevatorSDKServerGetElevatorPageDTO;
import com.itlong.cloud.POJO.PO.PropertyRoleProjectPO;
import com.itlong.cloud.POJO.VO.sdk.CloudEntranceSDKEntranceInfoVO;
import com.itlong.cloud.sdk.dao.ICloudElevatorSDKRobotDao;
import com.itlong.cloud.sdk.dao.ICloudEntranceSDKDao;
import com.itlong.cloud.sdk.service.ICloudEntranceSDKService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      云门禁SDK设备服务接口实现类
 * <desc/>
 *
 * @createDate 2019/03/14
 */
@Service
public class CloudEntranceSDKServerImpl implements ICloudEntranceSDKService {
    private static final Logger LOG = LoggerFactory.getLogger(CloudEntranceSDKServerImpl.class);

    @Autowired
    private ICloudElevatorSDKRobotDao iCloudElevatorSDKRobotDao;

    @Autowired
    private ICloudEntranceSDKDao iCloudEntranceSDKDao;

    private static final int DEVICE_AUTH_STATUS_YES = 1;
    /**
     * <desc>
     *      查询项目下门禁设备唯一码列表
     * </desc>
     *
     * @param cloudElevatorSDKServerGetElevatorPageDTO
     * @return 云门禁SDK查询项目下设备唯一码列表VO
     * @author Qiang.S
     * @createDate 2019/03/14
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<String> getEntranceKey(CloudElevatorSDKServerGetElevatorPageDTO cloudElevatorSDKServerGetElevatorPageDTO) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(cloudElevatorSDKServerGetElevatorPageDTO);
        List<String> list = null;
        if ("4".equals(cloudElevatorSDKServerGetElevatorPageDTO.getRoleId())) {//独立开发者
            PropertyRoleProjectPO propertyRoleProjectPO = iCloudElevatorSDKRobotDao.getOriginalProjectInfoByRole4(params);
            if (propertyRoleProjectPO != null) {
                if (1==propertyRoleProjectPO.getCloudEntranceApi()) {
                    // 从API授权设备信息中解析出deviceId
                    String deviceAuthCloudEntrance = propertyRoleProjectPO.getDeviceAuthCloudEntrance();
                    if (StringUtils.isNotBlank(deviceAuthCloudEntrance)) {
                        list = new ArrayList<>();
                        int i = deviceAuthCloudEntrance.indexOf("1");
                        while (i != -1) {
                            int deviceInt = i + 1;
                            String deviceId = StringHandlerUtil.formatStrLen(deviceInt + "", "0", 4, false, false);
                            list.add(cloudElevatorSDKServerGetElevatorPageDTO.getProjectId() + "2" + deviceId);
                            i = deviceAuthCloudEntrance.indexOf("1", deviceInt);
                        }
                    }
                }
            }
        } else if ("7".equals(cloudElevatorSDKServerGetElevatorPageDTO.getRoleId()) || "1".equals(cloudElevatorSDKServerGetElevatorPageDTO.getRoleId())) {//机器人开发者及工程人员
            params.put("deviceType",cloudElevatorSDKServerGetElevatorPageDTO.getDeviceType());
            if (DEVICE_AUTH_STATUS_YES == cloudElevatorSDKServerGetElevatorPageDTO.getRobotDeviceAuthStatus()){
                list = iCloudElevatorSDKRobotDao.getRobotDevelopElevatorKey(params);
            }else {
                list = iCloudEntranceSDKDao.getEntranceList(cloudElevatorSDKServerGetElevatorPageDTO.getProjectId());
            }

        }
        return list;
    }

    public static void main(String[] args){
        String deviceAuthCloudEntrance = "11111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ";
        List<String> list = new ArrayList<>();
        int i = deviceAuthCloudEntrance.indexOf("1");
        while (i != -1) {
            int deviceInt = i + 1;
            String deviceId = StringHandlerUtil.formatStrLen(deviceInt + "", "0", 4, false, false);
            list.add("00001976" + "2" + deviceId);
            i = deviceAuthCloudEntrance.indexOf("1", deviceInt);
        }
        System.out.println(list.toString());
    }

    /**
     * <desc>
     *      根据设备唯一码查询门禁信息
     * </desc>
     *
     * @param cloudElevatorSDKGetByDeviceUniqueDTO
     * @return 根据设备唯一码查询门禁信息VO
     * @author Qiang.S
     * @createDate 2019/03/14
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<CloudEntranceSDKEntranceInfoVO> getEntranceInfo(CloudElevatorSDKGetByDeviceUniqueDTO cloudElevatorSDKGetByDeviceUniqueDTO) throws Exception {
        List<String> deviceUniques = StringHandlerUtil.splitStringList(cloudElevatorSDKGetByDeviceUniqueDTO.getDeviceUnique());
        List<CloudEntranceSDKEntranceInfoVO> entranceInfo = iCloudEntranceSDKDao.getEntranceInfo(deviceUniques);
            if (entranceInfo.size()!=0){
                for (CloudEntranceSDKEntranceInfoVO EntranceInfoVO : entranceInfo) {
                    if (!EntranceInfoVO.equals(null)){
                        EntranceInfoVO.setUnitName(EntranceInfoVO.getUnitName()==null?"":EntranceInfoVO.getUnitName());
                        EntranceInfoVO.setBuildName(EntranceInfoVO.getBuildName()==null?"":EntranceInfoVO.getBuildName());
                    }

                }
            }
        return entranceInfo;
    }

    /**
     * <desc>
     *      获取开发者拥有的电梯门禁权限
     * </desc>
     *
     * @param userId 开发者id
     * @return 开发者拥有的设备权限
     * @Author Zhu.yj
     * @createDate  2020/9/8
     */
    @Override
    public Map<String, Object> getDeveloperAuth(String userId, String projectId) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("projectId", projectId);
        return iCloudElevatorSDKRobotDao.getDeveloperAuth(params);
    }

}
